/*
 * @Author: your name
 * @Date: 2020-06-30 15:44:20
 * @LastEditTime: 2021-03-23 10:51:27
 * @LastEditors: wangwangwang
 * @Description: In User Settings Edit
 * @FilePath: \icps_baseWeb\vue.config.js
 */
const path = require('path')
const resolve = dir => path.join(__dirname, dir)
const isProduction = process.env.NODE_ENV === 'production';
const Happypack = require('happypack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg|png)(\?.*)?$/i;
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const externals = process.env.NODE_ENV == 'development' ? {} : {
  axios: 'axios',
}
/**图片压缩
 * cnpm install image-webpack-loader --save-dev
*/
/**
 * 多页面配置
 * */
let objectProject = {             // 配置多页面打包
  index: {
    entry: 'src/main.js', // 入口文件地址
  },
  pages: {
    entry: 'src/pages/main.js', // 入口文件地址
  },
}
let page = {}

let projectname = process.argv[3] // 获取build后面的参数确定执行哪个文件 例如： yarn run bulid index

if (process.env.NODE_ENV == 'development') {
  for (let a in objectProject) {
    page[a] = {
      entry: objectProject[a].entry,
      template: `public/${a}.html`,
      filename: `${a}.html`
    }
  }
} else {
  /**测试打包*/
  if (projectname == '--mode') {
    for (let a in objectProject) {
      page[a] = {
        entry: objectProject[a].entry,
        template: `public/${a}.html`,
        filename: `${a}.html`
      }
    }
  }
  /**单独模块打包*/
  else if (projectname) {
    page[projectname] = {
      entry: objectProject[projectname].entry,
      template: `public/${projectname}.html`,
      filename: `${projectname}.html`
    }
  }
  /**打包整个程序*/
  else {
    for (let a in objectProject) {
      page[a] = {
        entry: objectProject[a].entry,
        template: `public/${a}.html`,
        filename: `${a}.html`
      }
    }
  }
}

module.exports = {
  publicPath: './',
  productionSourceMap: true,//是否打包源码最终使用false
  outputDir: projectname ? 'dist/' + projectname : 'dist',
  assetsDir: 'static',
  pages: page,
  css: {
    requireModuleExtension: true,
    extract: true,
    sourceMap: true,
    loaderOptions: {
      sass: {
        prependData: `@import "@/app.scss";`
      },
      less: {
        javascriptEnabled: true
      }
    }
  },
  configureWebpack: config => {
    config.externals = externals
    config.devtool = 'source-map'
    config.plugins.push(
      new Happypack({
        loaders: ['babel-loader', 'vue-loader', 'url-loader'],
        cache: true,
        threads: 16
      })
    )
    if (isProduction) {
      config.plugins.push(
        new BundleAnalyzerPlugin(
          {
            analyzerMode: 'server',
            analyzerHost: '127.0.0.1',
            analyzerPort: 6000, // 运行后的端口号
            reportFilename: 'report.html',
            defaultSizes: 'parsed',
            openAnalyzer: true,
            generateStatsFile: false,
            statsFilename: 'stats.json',
            statsOptions: null,
            logLevel: 'info'
          }
        )
      )
      config.plugins.push(
        new CompressionWebpackPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8,
          deleteOriginalAssets: false,
        })
      )
      // config.plugins.push(
      //   new UglifyJsPlugin({
      //     uglifyOptions: {
      //       compress: {
      //         drop_console: true,
      //         drop_debugger: false,
      //         pure_funcs: ["console.log"]
      //       },
      //       mangle: false,
      //       output: {
      //         beautify: true
      //       }
      //     },
      //     sourceMap: false,
      //     parallel: true
      //   }),
      // )
    }
  },
  chainWebpack: config => {
    /**别名*/
    config.resolve.alias.set("@", resolve("src"));
    if (isProduction) {
      config.plugins.delete('prefetch');
      config.plugins.delete('preload');
      /** ============压缩图片 start============ */
      config.module
        .rule('images')
        .use('image-webpack-loader')
        .loader('image-webpack-loader')
        .options({ bypassOnDebug: true })
        .end()
      /**小于10K的图片内联*/
      const imagesRule = config.module.rule('images')
      imagesRule.uses.clear()
      imagesRule
        .test(/\.(png|jpe?g|gif|svg|webp)$/i)
        .use('file-loader')
        .loader('url-loader')
        .options({
          limit: 10240,
          fallback: {
            loader: 'file-loader',
            options: {
              outputPath: 'static/images'
            }
          }
        })
        .end()
      config.optimization.minimize(true)
    }

  },
  devServer: {
    port: 8080,
    open: true,
    disableHostCheck: true,
    proxy: {
      // // 方便本地联调开发
      '/dev/base': {
        // target: 'http://10.31.5.65/',
        target: 'http://10.31.5.66/',
        // target: 'http://10.31.6.162:8088/',
        // target: 'http://10.31.6.134:8088/',
        // target: 'http://10.31.6.130:8088/',
        // target: 'http://10.31.6.43:8088/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/base': '/base',
        }
      },
      // 过磅
      '/weight': {
        target: 'http://10.31.5.66/',
        // target: 'http://10.31.5.66/',
        // target: 'http://10.31.6.130:9001/',
        // target: 'http://10.31.6.132:9001/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/weight': '/weight',
        }
      },
      // 文件上传验证
      '/dev/resource/accessory': {
        target: 'http://10.31.5.66/',
        // target: 'http://10.31.6.130:9999/',
        // target: 'http://10.31.6.43:9999/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/resource/accessory': '/resource/accessory',
        }
      },
      // 文件上传
      '/resource/resources': {
        target: 'http://10.31.5.66/',
        // target: 'http://10.31.6.130:9999/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/resource/resources': '/resource/resources',
        }
      },
      // 图算导入
      '/icps/system/sign/basics/graphic/importList': {
        target: 'http://10.31.6.132:8070/',
        // target: 'http://10.31.6.130:9999/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/icps/system/sign/basics/graphic/importList': '/icps/system/sign/basics/graphic/importList',
        }
      },
      // 日志
      '/dev/logs': {
        target: 'http://10.31.5.66/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/logs': '/logs',
        }
      },
      // 调度维护
      '/dev/schedule': {
        target: 'http://10.31.6.43:8055/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/schedule': '/schedule',
        }
      },
      // //对外接口
      '/dev/third': {
        target: 'http://10.31.5.66/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/third': '/third'
        }
      },
      // 首页配置
      '/dev/analysis': {
        target: 'http://10.31.5.66/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/analysis': '/analysis',
        }
      },
      // 首页数据
      '/dev/message': {
        target: 'http://10.31.5.66/',
        // target: 'http://10.31.5.62:9022/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/message': '/message',
        }
      },
      // 导入供应商
      '/supplier/importData': {
        target: 'http://10.31.5.66/',
        // target: 'http://10.31.6.130:9999/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/supplier/importData': '/supplier/importData',
        }
      },
      // //积分的
      // '/dev/integral': {
      //   target: 'http://10.31.6.162:9002/',
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev/integral': '/integral'
      //   }
      // },
      // //签核的
      '/dev/workflow': {
        // target: 'http://10.31.5.65',
        // target: 'http://10.31.5.66',
        target: 'http://10.31.6.130:8089/',
        changeOrigin: true,
        pathRewrite: {
          '^/dev/workflow': '/workflow'
        }
      },
      // /**同步管理*/
      // '/dev/transfer': {
      //   target: 'http://10.31.5.66',
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev/transfer': '/transfer',
      //   }
      // },
      '/dev': {
        target: process.env.VUE_APP_API_HOST, // 这里写你自己的服务器域名
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev': '',
        }
      },
      '/file': {
        target: 'http://10.31.5.66/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/file': '/file'
        }
      },
    },
  },
}

